package cn.teduymdj.jxc.controller;


import cn.teduymdj.jxc.myAnnotation.LogAnnotation;
import cn.teduymdj.jxc.pojo.dto.GoodsTypeAddNewDTO;
import cn.teduymdj.jxc.pojo.entity.Log;
import cn.teduymdj.jxc.pojo.vo.GoodsTypeStandsVO;
import cn.teduymdj.jxc.service.GoodsService;
import cn.teduymdj.jxc.service.GoodsTypeService;
import cn.teduymdj.jxc.web.JsonResult;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品类别控制器
 */
@RestController
@RequestMapping("/goodsType")
public class GoodsTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 删除商品类别
     *
     * @param id 商品类别ID
     * @return
     */
    //日志保存注解,不用管:李柯辰
    @LogAnnotation(content = "删除商品类别", type = Log.DELETE_ACTION)
    @RequestMapping("/delete")
    public JsonResult<Void> delete(Long id) {

        goodsTypeService.delete(id);
        return JsonResult.ok();
    }

    /**
     * 添加商品类别
     *
     * @param name 类别名
     * @param pId  父类ID
     * @return
     */
    @LogAnnotation(content = "添加商品类别", type = Log.INSERT_ACTION)
    @RequestMapping("/save")
    public JsonResult<Void> save(@RequestBody GoodsTypeAddNewDTO goodsTypeAddNewDTO) {

        goodsTypeService.saveGoodsType(goodsTypeAddNewDTO);
        return JsonResult.ok();
    }

    /**
     * 查询所有商品类别
     *
     * @return easyui要求的JSON格式字符串
     */
    @LogAnnotation(content = "查询所有商品类别", type = Log.SELECT_ACTION)
    @RequestMapping("/loadGoodsType")
    public String loadCheckMenu(Long roleId) {
        return this.getAllGoodsType(-1L).toString(); //根节点默认从-1开始
    }

    /**
     * 递归查询所有商品类别
     *
     * @return
     */
    public JsonArray getAllGoodsType(Long parentId) {

        JsonArray array = this.getGoodSTypeByParentId(parentId);

        for (int i = 0; i < array.size(); i++) {

            JsonObject obj = (JsonObject) array.get(i);

            if (obj.get("state").getAsString().equals("open")) {//如果是叶子节点，不再递归
                continue;
            } else {//如果是根节点，继续递归查询
                obj.add("children", this.getAllGoodsType(obj.get("id").getAsLong()));
            }
        }
        return array;
    }

    /**
     * 根据父ID获取所有子商品类别
     *
     * @return
     */
    public JsonArray getGoodSTypeByParentId(Long parentId) {

        JsonArray array = new JsonArray();
        List<GoodsTypeStandsVO> goodsTypeStandsVOS = goodsTypeService.getAllGoodsTypeByParentId(parentId);

        //遍历商品类别
        for (GoodsTypeStandsVO goodsTypeStandsVO : goodsTypeStandsVOS) {

            JsonObject obj = new JsonObject();
            obj.addProperty("id", goodsTypeStandsVO.getId());//ID
            obj.addProperty("text", goodsTypeStandsVO.getName());//类别名称

            if (goodsTypeStandsVO.getState() == 1) {
                obj.addProperty("state", "closed"); //根节点
            } else {
                obj.addProperty("state", "open");//叶子节点
            }

            obj.addProperty("iconCls", "goods-type");//图标默认为自定义的商品类别图标
            JsonObject attributes = new JsonObject(); //扩展属性
            attributes.addProperty("state", goodsTypeStandsVO.getState());//就加入当前节点的类型
            obj.add("attributes", attributes);
            array.add(obj);
        }
        return array;
    }

}
